<?php

namespace App\Modules\Index\Services;


use  App\Modules\Index\Models\diningTime;
use DB;

class diningTimeService
{

    /**
     * 添加血糖
     * param array $data
     */
    public static function mealsTime($user_id){

        $data['mealsconf'] = diningTime::where('user_id',$user_id)->get()->toArray();
        $data['basic'] = DB::table('users')->where('id',$user_id)->select('height','weight')->get()->toArray();
        if(empty($data['mealsconf'])){

            $data['mealsconf'] = diningTimeService::defaultMeals();
        }

        return $data;


    }

    public static function defaultMeals(){
        $res = DB::table('dictionary')->where('key','standard')->orWhere('key','diningtime')->orderBy('key','asc')->get()->toArray();
        $arr = [];
        foreach($res as $k => $val){
            $arr[$val->key] = DB::table('dictionary')->where('dictionary_id',$val->id)->orderBy('key','asc')->get()->toArray();
        }

        $before_low = substr($arr['standard']['0']->value,0,strpos($arr['standard']['0']->value, '-'));
        $before_high = substr($arr['standard']['0']->value,strpos($arr['standard']['0']->value, '-')+1);
        $after_low = substr($arr['standard']['1']->value,0,strpos($arr['standard']['1']->value, '-'));
        $after_high = substr($arr['standard']['1']->value,strpos($arr['standard']['1']->value, '-')+1);
        $bad_low = substr($arr['standard']['2']->value,0,strpos($arr['standard']['2']->value, '-'));
        $bad_high = substr($arr['standard']['2']->value,strpos($arr['standard']['2']->value, '-')+1);

        $data[0]['meals'] = 1;
        $data[0]['start_time'] = substr($arr['diningtime']['0']->value,0,strpos($arr['diningtime']['0']->value, '-'));
        $data[0]['end_time'] = substr($arr['diningtime']['0']->value,strpos($arr['diningtime']['0']->value, '-')+1);
        $data[0]['before_low'] = $before_low;
        $data[0]['before_high'] = $before_high;
        $data[0]['after_low'] = $after_low;
        $data[0]['after_high'] = $after_high;

        $data[1]['meals'] = 2;
        $data[1]['start_time'] = substr($arr['diningtime']['1']->value,0,strpos($arr['diningtime']['1']->value, '-'));
        $data[1]['end_time'] = substr($arr['diningtime']['1']->value,strpos($arr['diningtime']['1']->value, '-')+1);
        $data[1]['before_low'] = $before_low;
        $data[1]['before_high'] = $before_high;
        $data[1]['after_low'] = $after_low;
        $data[1]['after_high'] = $after_high;

        $data[2]['meals'] = 3;
        $data[2]['start_time'] = substr($arr['diningtime']['2']->value,0,strpos($arr['diningtime']['2']->value, '-'));
        $data[2]['end_time'] = substr($arr['diningtime']['2']->value,strpos($arr['diningtime']['2']->value, '-')+1);
        $data[2]['before_low'] = $before_low;
        $data[2]['before_high'] = $before_high;
        $data[2]['after_low'] = $after_low;
        $data[2]['after_high'] = $after_high;

        $data[3]['meals'] = 4;
        $data[3]['start_time'] = substr($arr['diningtime']['3']->value,0,strpos($arr['diningtime']['3']->value, '-'));
        $data[3]['end_time'] = substr($arr['diningtime']['3']->value,strpos($arr['diningtime']['3']->value, '-')+1);
        $data[3]['before_low'] = $bad_low;
        $data[3]['before_high'] = $bad_high;

        return $data;
    }


}